package cn.kys.tools.controller;

import cn.kys.logging.aop.log.EnableKysLog;
import cn.kys.tools.domain.EmailConfig;
import cn.kys.tools.domain.EmailVo;
import cn.kys.tools.service.EmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**功能描述：邮箱管理模块控制层
 * @Auther:http://www.kys.cn
 * @Date:2020/3/31
 * @Description:cn.kys.tools.controller
 * @version:1.0
 */
@RestController
@RequestMapping("api/email")
@Api(tags = "工具：邮件管理")
public class EmailController {

    private final EmailService emailService;

    public EmailController(EmailService emailService) {
        this.emailService = emailService;
    }

    @EnableKysLog("查询邮箱配置")
    @GetMapping
    @ApiOperation("查询邮箱配置")
    public ResponseEntity<Object> get(){
        return new ResponseEntity<>(emailService.find(), HttpStatus.OK);
    }

    @EnableKysLog("配置邮件")
    @PutMapping
    @ApiOperation("配置邮件")
    public ResponseEntity<Object> emailConfig(@Validated @RequestBody EmailConfig emailConfig){
        emailService.update(emailConfig,emailService.find());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @EnableKysLog("发送邮件")
    @PostMapping
    @ApiOperation("发送邮件")
    public ResponseEntity<Object> send(@Validated @RequestBody EmailVo emailVo) throws Exception {
        emailService.send(emailVo,emailService.find());
        return new ResponseEntity<>(HttpStatus.OK);
    }


}
